using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_GmOutsideCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_GmOutsideCell
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExText text { protected set; get; }
            public IQIGame.Onigao.Framework.ExToggle toggle { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("text", out var __tbv0);
                this.text = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExToggle>("toggle", out var __tbv1);
                this.toggle = __tbv1;
            }


            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields

        private CfgGmPanelList cfg;
        private Action<bool, CfgGmPanelList> onOpenClose;

        #endregion fields

        #region properties

        protected UIB_UIN_GmOutsideCell ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_GmOutsideCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.toggle.onValueChanged.AddListener(this.OnToggleValueChange);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        public void Show(CfgGmPanelList cfg, Action<bool, CfgGmPanelList> funcOpenClose)
        {
            this.cfg = cfg;
            this.onOpenClose = null;

            this.ui.text.text = cfg.Name;
            bool isOn = DevelopSettingModule.Instance.GetIsOn(cfg.Type);
            SetToggleIsOn(isOn);

            this.onOpenClose = funcOpenClose;
        }

        private void OnToggleValueChange(bool value)
        {
            this.onOpenClose?.Invoke(value, this.cfg);
        }

        public void SetToggleIsOn(bool isOn)
        {
            this.ui.toggle.isOn = isOn;
        }
        #endregion methonds
    }
}
